﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrainingCenterSystem.VocabularyManagement
{
    /// <summary>
    /// Represents a collection handler
    /// </summary>
    public class CollectionHandler
    {
        #region private members
        private List<VocabularyCollection> _currentCollections;

        private int _currentIndex;
        
        #endregion

        #region setters and getters
        /// <summary>
        /// Gets a list of all loaded collections.
        /// </summary>
        public List<VocabularyCollection> CurrentCollections
        {
            get { return _currentCollections; }
        }

        /// <summary>
        /// Gets or sets the index of the currently active collection.
        /// </summary>
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set { _currentIndex = value; }
        }

        #endregion

        #region constructor(s)
        /// <summary>
        /// Creates a collection handler
        /// </summary>
        public CollectionHandler()
        {
            _currentCollections = new List<VocabularyCollection>();
            _currentIndex = -1;
        }

        #endregion

        #region methods

        /// <summary>
        /// retrieves a single collection based on a given index
        /// </summary>
        /// <param name="index">the index of the collection</param>
        /// <returns>the specific collection</returns>
        public VocabularyCollection GetCollection(int index)
        {
            try
            {
                _currentIndex = index;
                return _currentCollections[index];                
            }
            catch (IndexOutOfRangeException ex)
            {
                return null;
            }
        }

        public VocabularyCollection GetCollectionByFileName(string filename)
        {
            for (int i = 0; i < _currentCollections.Count; i++)
            {
                if (_currentCollections[i].FileName == filename)
                    return _currentCollections[i];
            }
                return null;
        }

        /// <summary>
        /// Creates a new collection
        /// </summary>
        /// <param name="collName">The name of the collection</param>
        /// <param name="collUser">The creator of the collection</param>
        /// <param name="collAccess">The access type of the collection</param>
        /// <param name="collDescription">The description of  the collection</param>
        public void CreateCollection(string collName, UserManagement.User collUser, VocabularyManagement.CollectionAccess collAccess, string collDescription)
        {
            VocabularyCollection coll=new VocabularyCollection(collName,collUser,collAccess,collDescription);
            _currentCollections.Insert(0,coll);
            _currentIndex = 0;
        }

        /// <summary>
        /// Adds a collection to the list of currently open collections
        /// </summary>
        /// <param name="coll">the collection</param>
        public void AddCollection(VocabularyCollection coll)
        {
            _currentCollections.Insert(0, coll);
            _currentIndex = 0;
        }

        public void SwitchCollection(int index)
        {
            _currentIndex = index;
        }

        public void RemoveCurrentCollection()
        {
            _currentCollections.RemoveAt(_currentIndex);
        }
        #endregion

    }
}
